/*
 *  Copyright (C) 1993  Robert Davis
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of Version 2, or any later version, of 
 *  the GNU General Public License as published by the Free Software 
 *  Foundation.
 */


static char RCSId[]="$Id: DataFilePane.m,v 1.7 1993/05/30 09:10:07 davis Exp $";

#import <appkit/Application.h>
#import <appkit/Button.h>
#import <appkit/FormCell.h>
#import <appkit/Matrix.h>
#import <appkit/Panel.h>
#import <appkit/TextFieldCell.h>
#import <appkit/View.h>

#import <objc/NXStringTable.h>

#import "DataFilePane.h"
#import "DataOptionsPanel.h"
#import "FunctionObject.h"
#import "Status.h"

@implementation DataFilePane

- init
{
    [super init];

    [NXApp loadNibSection: "DataFilePane.nib"
		    owner: self
		withNames: NO
		 fromZone: [self zone]];

    return self;
}



- setDetailed:sender
{
    struct coldat	*cd = [[doc function] columnData];
    BOOL		isDetailed = [sender state];

    if (isDetailed != cd->isOn) {
	cd->isOn = isDetailed;
	[status reportSettingsChange:self];
	if ([self forceUpdateStatus:status doc:doc])
	    [[infoField superview] display];
    }

    return self;
}



- setColumnsData:sender
{
    FunctionObject	*f = [doc function];
    struct coldat	*columnData = [f columnData];
    int			value = [[sender selectedCell] intValue];


    if ((value <= columnData->number) && (value > 0)) {

	switch ([sender selectedTag]) {
	case XCOL:		columnData->x = value; break;
	case YCOL:		columnData->y = value; break;
	case YDELTACOL:		columnData->yDelta = value; break;
	case YLOWCOL:		columnData->yLow = value; break;
	case YHIGHCOL:		columnData->yHigh = value; break;
	case BOXWIDTHCOL:	columnData->boxWidth = value; break;
	}

	[status reportSettingsChange:self];
    }

    if ([self forceUpdateStatus:status doc:doc])
	[[infoField superview] display];	/* Display the box */
    return self;
}


- setYDiff:sender
{
    BOOL		useDelta = ([sender selectedTag] == USE_DELTA);
    FunctionObject	*f = [doc function];
    struct coldat	*columnData = [f columnData];

    if (useDelta != columnData->useYDelta) {
	columnData->useYDelta = useDelta;
	columnData->useYLow = !useDelta;
	columnData->useYHigh = !useDelta;
	[status reportSettingsChange:self];
    }

    if ([self forceUpdateStatus:status doc:doc])
	[[infoField superview] display];	/* Display the box */
    return self;
}


- (BOOL)updateStatus:aStatus doc:aDoc
{
    FunctionObject	*f;

    [super updateStatus:aStatus doc:aDoc];

    f = [doc function];

    if (f) {

	struct coldat	*c = [f columnData];
	char		info[1023];
	BOOL		enabled = c->isOn;
	int		style = [f style];
	Window		*viewWindow;

	[(viewWindow = [view window]) disableDisplay];

	[detailedButton setState:enabled];

	if (c->number == 1)
	    strcpy (info, [stringSet valueForStringKey:"columnsInfoSingular"]);
	else
	    sprintf (info, [stringSet valueForStringKey:"columnsInfoPlural"],
		     c->number);

	[infoField setStringValue: info];
	[infoField setEnabled: enabled];
	[infoLine2 setEnabled: enabled];

	[xColField setEnabled:(enabled && c->useX)];
	[xColField setIntValue:c->x];
	[yColField setEnabled:(enabled && c->useY)];
	[yColField setIntValue:c->y];

	enabled = enabled && (c->number > 2);

	[boxWidthColField setEnabled:(enabled && c->useBoxWidth &&
				      ((style == FUNCTION_BOXES) ||
				       (style == FUNCTION_BOXERRORBARS)))];
	[boxWidthColField setIntValue:c->boxWidth];


	enabled = enabled && ((style == FUNCTION_ERRORBARS) ||
			      (style == FUNCTION_BOXERRORBARS));

	[colRadioMatrix setEnabled:enabled];
	[colRadioMatrix selectCellWithTag:c->useYDelta? USE_DELTA :!USE_DELTA];
	[yDeltaColField setEnabled:(enabled && c->useYDelta)];
	[yDeltaColField setIntValue:c->yDelta];
	[yLowColField setEnabled:(enabled && c->useYLow)];
	[yLowColField setIntValue:c->yLow];
	[yHighColField setEnabled:(enabled && c->useYHigh)];
	[yHighColField setIntValue:c->yHigh];

	[viewWindow reenableDisplay];

    }

    return YES;
}


// Shuts up the compiler about unused RCSId
- (const char *) rcsid
{
    return RCSId;
}

@end
